using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

using UnityEngine;


namespace GJXQGame
{
    public class GUIManager : Singleton<GUIManager>
    {
        public GameObject UIRoot;
        public GameObject AnchorRoot;
        public GameObject AnchorBottomLeft;
        public GameObject AnchorBottomRight;
        public GameObject AnchorCustom;
        public GameObject AnchorRight;
        public GameObject AnchorTopLeft;
        public GameObject AnchorTopRight;
        public GameObject Background;
        public GameObject GUICamera;
        public Vector3 GUIWinScaleRatio;
        private Dictionary<GUI_ID, GUIBase> mGUIMap;
        private Dictionary<GUI_ID, GUIBase> mShowGUIMap;
        private bool mShowGUIMapModifyFlag;

        private float     m_fUpdateTime;

        public GUIBase this[GUI_ID guiID]
        {
            get
            {
                if (!mGUIMap.ContainsKey(guiID))
                {
                    GameLog.LogError("[GUI]Trying to get GUI using an invalid id: " + guiID);
                    return null;
                }

                return mGUIMap[guiID];
            }
        }

        public List<GUI_ID> ShownGUI
        { get { return mShowGUIMap.Keys.ToList(); } }

        public void Destory()
        {
            //this.m_c_CreateRole.Destory();

            Resources.UnloadUnusedAssets();
        }

        public GUIBase GetMessage()
        {
            return null;
        }

        public GUIBase GetGUI(GUI_ID guiID)
        {
            GUIBase base2 = null;
            if (mGUIMap.ContainsKey(guiID))
                base2 =  mGUIMap[guiID];
            return base2;
        }



        //    public GUICreateRole GetGUICreateRole()
        //    {
        //        return this.m_c_CreateRole;
        //    }



        public void HidenAll()
        {
            //        if (this.m_c_CreateRole.IsShow())
            //        {
            //            this.m_c_CreateRole.Hiden();
            //        }

        }

        public void HidenAllTips()
        {

        }

        public void Initialize()
        {
            InitGUIWinScaleRatio();
            mGUIMap = new Dictionary<GUI_ID, GUIBase>();
            mShowGUIMap = new Dictionary<GUI_ID, GUIBase>();
            InitGameObject();

            RegisterGUI(new MainWin(this));
            RegisterGUI(new PlayInfoWin(this));
            
        }

        private void InitGUIWinScaleRatio()
        {
            //float screenScaleXy = Global.ScreenSize.x / Global.ScreenSize.y;
            //float scaleGUI = screenScaleXy / Global.DEFAULT_XY_ASPECT_RATIO;
            //if (screenScaleXy < Global.DEFAULT_XY_ASPECT_RATIO)
            //    GUIWinScaleRatio = new Vector3(scaleGUI, scaleGUI, scaleGUI);
            //else
            //    GUIWinScaleRatio = Vector3.one;
        }

        private void InitGameObject()
        {
            UIRoot = GameObject.Find("GUIRoot");
            GUICamera = GameObject.Find("GUIRoot/Camera");
        }

        public GameObject GetRoot()
        {
            if (UIRoot==null)
            {
                InitGameObject();
            }
            return UIRoot;
        }

        public void RegisterGUI(GUIBase guiBase)
        {
            guiBase.Initialize();
            mGUIMap.Add(guiBase.ID, guiBase);
        }

        public void RegisterShowGUI(GUIBase guiBase)
        {
            if (mShowGUIMap.ContainsKey(guiBase.ID)) return;

            mShowGUIMap.Add(guiBase.ID, guiBase);
            mShowGUIMapModifyFlag = true;
        }

        public void UnRegisterShowGUI(GUIBase guiBase)
        {
            mShowGUIMap.Remove(guiBase.ID);
            mShowGUIMapModifyFlag = true;
        }

        public void Start()
        {
                 
        }

//        public bool FixedUpdate()
//        {
//            this.m_fUpdateTime = GameGUIUtility.GameRunTime();
//            foreach (KeyValuePair<GUI_ID, GUIBase> gui in mGUIMap)
//                if (gui.Value.IsShow()) gui.Value.FixedUpdate();
//
//            return true;
//        }

        public bool Update()
        {
            mShowGUIMapModifyFlag = false;
            foreach (KeyValuePair<GUI_ID, GUIBase> gui in mShowGUIMap)
            {
                if (gui.Value.IsShow()) gui.Value.Update();
                if (mShowGUIMapModifyFlag) return true;
            }

            return true;
        }
    }
}